import os.path


class SignClass:
    def __init__(self):
        self.java_home = ""
        self.jar_path = ""
        self.store_file = ""
        self.store_pass = ""
        self.key_alias = ""
        self.key_pass = ""
        self.save_file_path = ""
        self.input_file_path = ""
        self.array_command = [
            "{java_home}",
            "-jar",
            "{jar_path}",
            "sign",
            "-verbose",
            "--ks",
            "{store_file}",
            "--v1-signing-enabled",
            "true",
            "--v2-signing-enabled",
            "true",
            "--ks-pass",
            "pass:{store_pass}",
            "--ks-key-alias",
            "{key_alias}",
            "--key-pass",
            "pass:{key_pass}",
            "--out",
            "{save_file_path}",
            "{input_file_path}"
        ]

    def run(self):
        str_command = " ".join(self.array_command)
        command = str_command.replace("{java_home}", '"%s"' % os.path.join(self.java_home, "bin", "java")) \
            .replace("{jar_path}", os.path.join(self.jar_path, "lib", "apksigner.jar")) \
            .replace("{store_file}", self.store_file) \
            .replace("{store_pass}", self.store_pass) \
            .replace("{key_alias}", self.key_alias) \
            .replace("{key_pass}", self.key_pass) \
            .replace("{save_file_path}", self.save_file_path) \
            .replace("{input_file_path}", self.input_file_path)
        if os.system(command) == 0:
            print(f"渠道加固包 {os.path.basename(self.input_file_path)} 签名完成，保存地址：{self.save_file_path}")
        else:
            print(f"渠道加固包 {os.path.basename(self.input_file_path)} 签名失败")
